// sd_liferuning/pages/constmer/balance-recharge/index.js
var api = require("../../../api.js"), app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    money: 0,
    balance: 0,
    webRoot: app.webRoot,
    yue: 0,
    distribution: ''
  },
  onShow: function () {
    let that = this;
    app.request({
      url: api.bank.get_maxmoney,
      method: 'post',
      data: {
        type: 1,
        default: 1
      },
      state: 1,
      success: function (e) {
        console.log(e)
        if (e.code == 1) {
          that.setData({
            data: e.data,
            //bank_number: e.data.banknum.slice(-4),
            balance: e.data.price,
          })
        }
      }
    })
  },
  changeInputData: function (e) {
    let that = this;
    let inputName = e.currentTarget.dataset.name;
    let inputVlaue = e.detail.value;
    if (inputName == "money") {
      that.setData({
        money: inputVlaue
      });
    }
  },
  sendRequest: function () {
    let _this = this;
    let _money = _this.data.money;
    let _balance = _this.data.balance;
    if (_money == "") {
      wx.showToast({
        title: '请正确填写金额',
        icon: 'none',
        mask: true
      })
      return false;
    } else if (Number(_money) > Number(_balance)) {
      wx.showToast({
        title: '金额大于可提现金额',
        icon: 'none',
        mask: true
      })
      return false;
    } else if (_money < 1) {
      wx.showToast({
        title: '提现金额至少1元',
        icon: 'none',
        mask: true
      })
      return false;
    }
    wx.showLoading({
      mask: true
    })
    if (this.data.distribution == 1) {
      app.request({
        url: api.user.apply,
        method: 'post',
        data: {
          price: _money,
          type: 0 //提现到银行卡
        },
        success: function (rs) {
          wx.hideLoading()
          console.log('提现申请结果', rs)
          if (rs.code == 1) {
            wx.showToast({
              title: rs.msg,
              icon: 'none',
              mask: true,
              duration: 2000,
            })
            setTimeout(function () {
              wx.navigateBack({
                delta: 1
              })
            }, 2000)
          } else {
            wx.showToast({
              title: rs.msg,
              icon: 'none',
              mask: true
            })
          }
        }
      })
    } else if (this.data.distribution == 2) {
      app.request({
        url: api.distribution.apply,
        method: 'post',
        data: {
          price: _money,
          type: 0 //提现到银行卡
        },
        success: function (rs) {
          wx.hideLoading()
          console.log('提现申请结果', rs)
          if (rs.code == 1) {
            wx.showToast({
              title: rs.msg,
              icon: 'none',
              mask: true,
              duration: 2000,
            })
            setTimeout(function () {
              wx.navigateBack({
                delta: 1
              })
            }, 2000)
          } else {
            wx.showToast({
              title: rs.msg,
              icon: 'none',
              mask: true
            })
          }
        }
      })
    }
    //  0提现到余额 1银行卡 2 支付宝 参数nickname：昵称  alipay：支付宝账号

  },
  onLoad: function (options) {
    console.log(options)
    var that = this
    that.setData({
      yue: options.yue,
      distribution: options.distribution
    });
  }
})